<template>
  <div class="watch-demo5">
    <h1>情况5:监视同时监听多个属性</h1>
    <h2>姓名：{{ person.name }}</h2>
    <h2>年龄：{{ person.age }}</h2>
    <h2>汽车：{{ person.car.c1 }},{{ person.car.c2 }}</h2>
    <button @click="changeName">修改名字</button>
    <button @click="changeAge">修改年龄</button>
    <button @click="changeCar1">修改第一台车</button>
    <button @click="changeCar2">修改第二台车</button>
    <button @click="changeAllCar">修改所有车</button>
  </div>
</template>

<script lang="ts" setup name="WatchDemo5">
import { reactive, watch } from "vue";
let person = reactive({
  name: "张三",
  age: 18,
  car: {
    c1: "奔驰",
    c2: "宝马",
  },
});
function changeName() {
  person.name += "~";
}
function changeAge() {
  person.age += 1;
}
function changeCar1() {
  person.car.c1 += "~";
}
function changeCar2() {
  person.car.c2 += "~";
}
function changeAllCar() {
  person.car = { c1: "五菱", c2: "蔚来" };
}
watch(
  [
    () => {
      return person.name;
    },
    () => {
      return person.car.c1;
    },
  ],
  (newValue, oldValue) => {
    //监听多个值，这里的新旧值式数组，按照监听的顺序存放数据
    console.log("监听到属性变化", newValue, oldValue);
  },
  { deep: true }
);
</script>

<style>
.watch-demo5 {
  background-color: skyblue;
  box-shadow: 0 0 10px;
  border-radius: 10px;
  padding: 20px;
}
</style>